package com.softer.chengxin.corp.service.impl;

import com.softer.chengxin.common.domin.*;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.corp.dao.CorpDao;
import com.softer.chengxin.corp.dao.ManagerDao;
import com.softer.chengxin.corp.domin.Corp;
import com.softer.chengxin.corp.domin.CorpDTO;
import com.softer.chengxin.corp.domin.ProDTO;
import com.softer.chengxin.corp.domin.TrainVO;
import com.softer.chengxin.corp.service.ManagerService;
import com.softer.chengxin.person.domin.Person;
import com.softer.chengxin.person.domin.PersonVO;
import com.softer.chengxin.score.dao.SDManagerDao;
import com.softer.chengxin.score.util.ScoreUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangxinrui
 * @date 2018/10/09
 */
@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerDao managerDao;
    @Autowired
    private CorpDao corpDao;
    @Autowired
    private ScoreUtil scoreUtil;
    @Autowired
    private SDManagerDao sdManagerDao;

    /**
     * 企业下主要人员
     *
     * @param qyId   企业id
     * @param duties 职位
     * @return 结果
     */
    @Override
    public Person findZYRY(String qyId, Integer duties) {
        return managerDao.findZYRY(qyId, duties);
    }

    /**
     * 企业下从业人员
     *
     * @param qyId  企业id
     * @param page  页码
     * @param name  姓名
     * @param depId 部门
     * @return 结果
     */
    @Override
    public ManagerPageResult findCYRY(String qyId, Page page, String name, String depId) {
        ManagerPageResult mpr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        int count = managerDao.findCYRYCount(qyId, name, depId);
        if (count <= NumberEnum.ZERO.getNum()) {
            return common(mpr, jr);
        }
        List<PersonVO> list = managerDao.findCYRY(qyId, page, name, depId);
        jr.setDs(list);
        mpr.setCount(count);
        mpr.setJson(jr);
        return mpr;
    }

    /**
     * 查询企业信息 条件查询
     *
     * @param qyName      企业名
     * @param district    区域
     * @param cxDateBegin 查询录入开始时间
     * @param cxDateEnd   查询录入结束时间
     * @param state       状态
     * @param page        页码
     * @param loginName   管理账号名
     * @return 结果
     */
    @Override
    public ManagerPageResult findCorpByCondition(String qyName, Integer district, String cxDateBegin, String cxDateEnd, Integer state, Page page, String loginName) {
        ManagerPageResult mpr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        //获取管理区域
        String area = null;
        if (!Common.ADMIN.equals(loginName)) {
            area = corpDao.getDistinct(loginName);
        }
        int count = managerDao.findCorpByConditionCount(qyName, district, cxDateBegin, cxDateEnd, state, area);
        if (count <= NumberEnum.ZERO.getNum()) {
            return common(mpr, jr);
        }
        List<Corp> list = managerDao.findCorpByCondition(qyName, district, cxDateBegin, cxDateEnd, state, page, area);
        for (Corp corp : list) {
            String districtName = corp.getDistrictName() + corp.getStreetName();
            corp.setDistrictName(districtName);
        }
        jr.setDs(list);
        mpr.setCount(count);
        mpr.setJson(jr);
        return mpr;
    }

    /**
     * 查找该管理账号下从业人员
     *
     * @param loginName 登录名
     * @param name      姓名
     * @param qyName    企业姓名
     * @param page      页码
     * @return 结果
     */
    @Override
    public ManagerPageResult findPersonByCondition(String loginName, String name, String qyName, Page page) {
        ManagerPageResult mpr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        String area = null;
        if (!Common.ADMIN.equals(loginName)) {
            area = corpDao.getDistinct(loginName);
        }
        int count = managerDao.findPersonByConditionCount(area, name, qyName);
        if (count <= NumberEnum.ZERO.getNum()) {
            return common(mpr, jr);
        }
        List<PersonVO> list = managerDao.findPersonByCondition(area, name, qyName, page);
        jr.setDs(list);
        mpr.setCount(count);
        mpr.setJson(jr);
        return mpr;
    }

    /**
     * 查询企业继续教育情况
     *
     * @param corpId 企业id
     * @param name   姓名
     * @return 结果
     */
    @Override
    public ManagerPageResult findTrain(Integer corpId, String name, Page page) {
        ManagerPageResult mpr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        int count = managerDao.findTrainCount(corpId, name);
        if (count < NumberEnum.ZERO.getNum()) {
            return common(mpr, jr);
        }
        List<TrainVO> list = managerDao.findTrain(page, corpId, name);
        jr.setDs(list);
        mpr.setCount(count);
        mpr.setJson(jr);
        return mpr;
    }

    /**
     * 获取企业名列表
     *
     * @param loginName 登录名
     * @return 结果
     */
    @Override
    public List<CorpDTO> getCorpNameList(String loginName) {
        String area = null;
        if (!Common.ADMIN.equals(loginName)) {
            area = corpDao.getDistinct(loginName);
        }
        return managerDao.getCorpNameList(area);
    }

    /**
     * 获取项目名列表
     *
     * @param corpId 登录名
     * @return 结果
     */
    @Override
    public List<ProDTO> getProjectNameList(Integer corpId) {
        return managerDao.getProjectNameList(corpId);
    }

    /**
     * 获取企业详细信息
     *
     * @param id 企业id
     * @return 结果
     */
    @Override
    public Corp getCorpDetail(Integer id) {
        Corp corp = managerDao.getCorpDetail(id);
        //企业注册地
        String districtName = corp.getDistrictName() + corp.getStreetName();
        corp.setDistrictName(districtName);
        return corp;
    }

    /**
     * 审批继续教育
     *
     * @param id     id
     * @param state  状态
     * @param yijian 意见
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult spTrain(Integer id, Integer state, String yijian) {
        //获取教育信息
        TrainVO trainVO = managerDao.findTrainById(id);
        //判断是否已经审批
        if (trainVO.getState() != 1) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "该信息已审批！");
        }
        //审批
        int ret = managerDao.spTrain(id, state, yijian);
        //查找企业id
        Integer corpId = managerDao.findCorpId(id);
        //重新算分
        scoreUtil.updateRecord(corpId);
        if (ret > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), Common.OPERATE_SUCCESS);
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), Common.OPERATE_FAIL);
    }

    /**
     * 查询企业继续教育清理
     *
     * @param loginName  登录名
     * @param page       页码
     * @param qyName     企业名
     * @param state      状态
     * @param department 培训部门
     * @return 结果
     */
    @Override
    public ManagerPageResult getTrainList(String loginName, Page page, String qyName, Integer state, Integer department) {
        ManagerPageResult mpr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        String area = null;
        if (!Common.ADMIN.equals(loginName)) {
            area = corpDao.getDistinct(loginName);
        }
        int num = managerDao.getTrainListCount(area, qyName, state,department);
        if (num <= NumberEnum.ZERO.getNum()) {
            return common(mpr, jr);
        }
        List<TrainVO> list = managerDao.getTrainList(area, qyName, state, page,department);
        mpr.setCount(num);
        jr.setDs(list);
        mpr.setJson(jr);
        return mpr;
    }

    /**
     * 继续教育审批按钮  点击后显示的列表
     *
     * @param page      页码
     * @param loginName 登录名
     * @return 结果
     */
    @Override
    public ManagerPageResult getSPTrainList(Page page, String loginName) {
        ManagerPageResult mpr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        String department = null;
        //获取该登录账号的管理级别
        Integer roleId = managerDao.getRoleId(loginName);
        String dicCode = sdManagerDao.getDicCodeByLoginName(loginName);
        if (String.valueOf(NumberEnum.THREE.getNum()).equals(dicCode)) {
            department = "1009001";
        } else if (String.valueOf(NumberEnum.FOUR.getNum()).equals(dicCode)) {
            department = "1009002";
        } else if (String.valueOf(NumberEnum.SIX.getNum()).equals(dicCode)) {
            department = "1009003";
        }
        String area = null;
        if (!Common.ADMIN.equals(loginName)) {
            area = corpDao.getDistinct(loginName);
        }
        int num = managerDao.getSPTrainListCount(department, area);
        if (num <= NumberEnum.ZERO.getNum()) {
            return common(mpr, jr);
        }
        List<TrainVO> list = managerDao.getSPTrainList(page, department, area);
        mpr.setCount(num);
        jr.setDs(list);
        mpr.setJson(jr);
        return mpr;
    }

    public ManagerPageResult common(ManagerPageResult managerPageResult, JsonResult jsonResult) {
        jsonResult.setDs(new ArrayList());
        managerPageResult.setJson(jsonResult);
        managerPageResult.setCount(NumberEnum.ZERO.getNum());
        return managerPageResult;
    }

}
